package com.topisv.tms.web;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.CustomersOrdenoSource;
import com.topisv.tms.entity.OrdenoSource;
import com.topisv.tms.entity.ShopSource;
import com.topisv.tms.service.CustomersOrdenoSourceService;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.OrdenoSourceService;
import com.topisv.tms.service.ShopSourceService;

/**
 * 商家合单设置
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/v2/customersOrdenoSource")
public class CustomersOrdenoSourceController {
	private static final Logger log = LoggerFactory.getLogger(CustomersOrdenoSourceController.class);
	
	@Autowired
	private CustomersOrdenoSourceService customersOrdenoSourceService;
	@Autowired
	private ShopSourceService shopSourceService;
	@Autowired
	private OrdenoSourceService ordenoSourceService;
	@Autowired
	private CustomersService  customersService;
	
	/**
	 * 显示设置的合单条件
	 * @param customerCode
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "viewsOrderSource/{customerCode}",method = RequestMethod.GET)
	public String views(@PathVariable(value="customerCode") String customerCode,Model model){
		try {
			Customers customers=customersService.findByCustomerCode(customerCode);
			model.addAttribute("customers", customers);
			List<CustomersOrdenoSource> customersOrdenoSources=customersOrdenoSourceService.findByCustomerCode(customerCode);
			if(customersOrdenoSources.isEmpty()){
				//得到平台
				List<ShopSource> shopSources=shopSourceService.findByCustomerCode(customerCode);
				Set<OrdenoSource> orderSourceSet=new HashSet<OrdenoSource>();
				for(ShopSource s:shopSources){
					OrdenoSource os=ordenoSourceService.findByOrdenoSourceCode(s.getOrdenoSourceCode());
					orderSourceSet.add(os);
				}
				model.addAttribute("orderSources", orderSourceSet);//平台
				model.addAttribute("action", "create");
				return "/customers/customerSourceForm";//还没开始设置
			}
			
			model.addAttribute("customersOrdenoSources", customersOrdenoSources);
			return "/customers/customerSourceList";
		} catch (Exception e) {
			return "redirect:/v2/customers/v2_show";
		}
	}
	
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable(value="id") Long id,Model model) {
		try {
			
			CustomersOrdenoSource customersOrdenoSource=customersOrdenoSourceService.findOne(id);
			Customers customers=customersService.findByCustomerCode(customersOrdenoSource.getCustomerCode());
			model.addAttribute("customers", customers);
			model.addAttribute("action", "create");
			model.addAttribute("customersOrdenoSource", customersOrdenoSource);
			
			//得到平台
			List<ShopSource> shopSources=shopSourceService.findByCustomerCode(customersOrdenoSource.getCustomerCode());
			Set<OrdenoSource> orderSourceSet=new HashSet<OrdenoSource>();
			for(ShopSource s:shopSources){
				OrdenoSource os=ordenoSourceService.findByOrdenoSourceCode(s.getOrdenoSourceCode());
				orderSourceSet.add(os);
			}
			model.addAttribute("orderSources", orderSourceSet);//平台
		} catch (Exception e) {
			log.error("error",e);
		}
		return "/customers/customerSourceForm";
	}
	
	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@Valid CustomersOrdenoSource customersOrdenoSource, RedirectAttributes redirectAttributes) {
		try {
			customersOrdenoSourceService.save(customersOrdenoSource);
		} catch (Exception e) {
			log.error("error",e);
			redirectAttributes.addFlashAttribute("message", e.getMessage());
		}
		return "redirect:/v2/customers/v2_show";
	}
}
